package study.ttl.exception;

import cn.dev33.satoken.exception.NotLoginException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import study.ttl.common.Result;
import study.ttl.common.enums.ResultCodeEnum;

import javax.security.sasl.SaslException;

@ControllerAdvice("study.ttl.controller")
@Slf4j
public class GlobalExceptionHandler {

    //统一异常处理@ExceptionHandler,主要用于Exception
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        log.error("全局异常：", e);
        return Result.error();
    }


    // 自定义运行时异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody//返回json串
    public Result error(CustomException e){
        log.error("异常信息：",e);
        return Result.error(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(NotLoginException.class)
    @ResponseBody
    public Result error(NotLoginException e, HttpServletRequest request){
        log.error("权限异常：",e);
        return Result.error(ResultCodeEnum.TOKEN_INVALID_ERROR.code,ResultCodeEnum.TOKEN_INVALID_ERROR.msg);
    }
}
